<template>
  <div>
    <h1>RightsView</h1>
    <el-tree
      :props="props"
      :load="loadNode"
      lazy
      show-checkbox
      @check-change="handleCheckChange"
    ></el-tree>
  </div>
</template>

<script>
export default {
  name: "RightsView",
  data() {
    return {
      /* 这里是用来配置数据对象的名字 */
      props: {
        label: "name",
        children: "zones",
      },
      count: 1,
    };
  },
  methods: {
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    /* 每点层级不勾选复选框都会执行一遍 */
    loadNode(node, resolve) {
      /* 第一层给一个数组数据 */
      if (node.level === 0) {
        return resolve([{ name: "region1" }, { name: "region2" }]);
      }
      if (node.level > 3) return resolve([]);
      /* 这个开关是用来判断给谁加子数据 */
      var hasChild;
      if (node.data.name === "region1") {
        hasChild = true;
      } else if (node.data.name === "region2") {
        hasChild = false;
      } else {
        hasChild = Math.random() > 0.5;
      }
      setTimeout(() => {
        var data;
        if (hasChild) {
          data = [
            {
              name: "zone" + this.count++,
            },
            {
              name: "zone" + this.count++,
            },
          ];
        } else {
          data = [];
        }
        resolve(data);
      }, 500);
    },
  },
};
</script>

<style>
</style>